package com.miao.hong.fang.teacher.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.miao.hong.fang.admin.BackupFileTO;
import com.miao.hong.fang.base.dao.BaseDAO;
import com.miao.hong.fang.base.dao.PaginationEnabler;
import com.miao.hong.fang.course.CourseTO;
import com.miao.hong.fang.teacher.TeacherTO;
import com.miao.hong.fang.util.SecurityUtil;
import com.miao.hong.fang.util.StringUtil;
import com.miao.hong.fang.util.pagination.ListAndCount;
import com.miao.hong.fang.util.pagination.PaginationContext;
import com.miao.hong.fang.util.pagination.PaginationSortOrderData;

public class TeacherDAOImpl extends BaseDAO implements TeacherDAO{

	public List getTeacherByCategory(String category) {
		
		DetachedCriteria criteria = DetachedCriteria.forClass(TeacherTO.class);
		criteria.add(Restrictions.eq("teachMajor", category));
		
		getHibernateTemplate().setCacheQueries(true);
		return getHibernateTemplate().findByCriteria(criteria);
	}
	
	public boolean validateTeacherName(String name) {
		
		List list = getHibernateTemplate().find("from TeacherTO where teacherName = ?", name);
		if(list.size() > 0) {
			return false;
		}
		return true;
	}
	
	public String addTeacher(TeacherTO teacherTO) {
		return (String) getHibernateTemplate().save(teacherTO);
	}
	
	public void saveTeacherImageToDB(BackupFileTO to) {
		save(to);
	}
	
	public ListAndCount searchTeacher(String teacherName, String teacherDesc, String teachMajor) {
		
		DetachedCriteria criteria = DetachedCriteria.forClass(TeacherTO.class);
	
		if (StringUtil.isNotEmpty(teacherName)) {
			criteria.add(Restrictions.like("teacherName", teacherName, MatchMode.ANYWHERE));	
		}
		if (StringUtil.isNotEmpty(teacherDesc)) {
			criteria.add(Restrictions.like("teacherDesc", teacherDesc, MatchMode.ANYWHERE));	
		}
		if (StringUtil.isNotEmpty(teachMajor)) {
			criteria.add(Restrictions.eq("teachMajor", teachMajor));	
		}
		
		getHibernateTemplate().setCacheQueries(true);
		return PaginationEnabler.queryForList(getHibernateTemplate(), criteria); 
	}
	
	public void deleteTeacher(String teacherId, String version) {
		
		//delete course with this teacher id, first remove from cache
		removeFromCache("select course_id from tbl_course where course_teacher = '" + teacherId + "'", CourseTO.class);
		String sql = "delete from tbl_course where course_teacher = ?";
		getJdbcTemplate().update(sql, new Object[]{teacherId});
		
		TeacherTO to = new TeacherTO();
		to.setTeacherId(teacherId);
		to.setVersion(Integer.valueOf(version));
		getHibernateTemplate().delete(to);
	}
	
	public TeacherTO getTeacherDetail(String teacherId) {
		return (TeacherTO) getHibernateTemplate().get(TeacherTO.class, teacherId);
	}

	public ListAndCount getCoursesByTeacher(String teacherId) {
		
		DetachedCriteria criteria = DetachedCriteria.forClass(CourseTO.class);
		criteria.add(Restrictions.eq("courseTeacher", teacherId));
		
		return PaginationEnabler.queryForList(getHibernateTemplate(), criteria);		
	}
	
	public void updateTeacher(TeacherTO teacherTO) {
		getHibernateTemplate().update(teacherTO);
	}
	
	public int updateTeacherMajor(String oldValue, String newValue) {
		
		String sql = "update tbl_teacher set TEACH_MAJOR = ? where TEACH_MAJOR = ?";
		Object[] args = {newValue, oldValue};
		return getJdbcTemplate().update(sql, args);
	}
	
	public ListAndCount getAllTeacher(String category) {
		DetachedCriteria criteria = DetachedCriteria.forClass(TeacherTO.class);
		if(StringUtil.isNotEmpty(category)) {
			criteria.add(Restrictions.eq("teachMajor", category));
		}
		PaginationSortOrderData data = PaginationContext.getPaginationSortOrderData();
		data.setPageSize(10);
		PaginationContext.setPaginationSortOrderData(data);
		return PaginationEnabler.queryForList(getHibernateTemplate(), criteria);
	}
	
	public BackupFileTO getBackUpFile(String id) {
		return (BackupFileTO) getHibernateTemplate().get(BackupFileTO.class, id);
	}
	public void deleteBackupFile(String id) {
		BackupFileTO to = new BackupFileTO();
		to.setId(id);
		getHibernateTemplate().delete(to);
	}
	
	public List getAllImages() {
		String hql = "SELECT new BackupFileTO(id, fileName) FROM BackupFileTO";
		List list = getHibernateTemplate().findByValueBean(hql, new Object());
		return list;
	}
	
	public byte[] getImage(String id) {
		
		byte[] result = null;
		try {
			BackupFileTO fileTO = (BackupFileTO) getHibernateTemplate().get(BackupFileTO.class, id);
			//return fileTO.getFile();
			return SecurityUtil.decryptImage(fileTO.getFile());			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}









